﻿<?php
class JswjggtzdController extends Zend_Controller_Action
{
	private $jswjggtzd;
	private $tools;

    public function init()
    {
        //验证
		//Ajax请求
		if($this->getRequest()->isXmlHttpRequest()){
			$this->_helper->layout->disableLayout();
			$this->_helper->viewRenderer->setNoRender();
		}
		//初始化
		$this->jswjggtzd = new Model_DbTable_Jswjggtzd();
		$this->tools     = new Model_Tools();
		
    }
	//文件列表
    public function indexAction()
    {
        $this->view->title = "技术文件更改通知单";
		$this->view->headTitle($this->view->title,'PREPEND');
		$this->view->files = $this->jswjggtzd->fetchAll();
    }
	
	//添加文件
    public function addAction()
    {
        $this->view->title = "添加通知单";
		$this->view->headTitle($this->view->title,'PREPEND');
	
		$form = new Form_AddJswjggtzd();
		$form->submit->setLabel('添加');
		$this->view->form = $form;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				
				$old_id       = $form->getValue('old_id');
				$drawing_num  = $form->getValue('drawing_num');
				$file_name    = $form->getValue('file_name');
				$file_content = $form->getValue('file_content');
				$use_for      = $form->getValue('use_for');
				$add_date     = $form->getValue('add_date');
				//生成目录及文件
				$new_name = date('YmdHis');
				$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$old_id.'-'.$new_name;
				$target_file = $new_name.'.jpg';
				mkdir($target_dir,0777);
				//扫描件
				$form->scan_img->addFilter('Rename',
										   array('target'    => $target_dir.'/'.$target_file,
										         'overwrite' => true));
				//PDF
				$form->pdf_file->addFilter('Rename',
										   array('target'    => $target_dir.'/'.$new_name.'.pdf',
										         'overwrite' => true));
				//接收文件
				$scan_img = $form->getValue('scan_img');
				$pdf_file = $form->getValue('pdf_file');
							
				$scan_uri = 'doc/jswjggtzd/'.$old_id.'-'.$new_name.'/'.$scan_img;		
				$pdf_uri  = 'doc/jswjggtzd/'.$old_id.'-'.$new_name.'/'.$pdf_file;
				
				//Zend_Debug::dump($formData,'formData');
				//存数据库
				$this->jswjggtzd->addFile($old_id,
									$drawing_num,
									$file_name,
									$file_content,
									$use_for,
									$add_date,
									$scan_uri,
									$pdf_uri);
				$this->_redirect('/jswjggtzd/index');
			}else{
				$form->populate($formData);
			}
		}
    }
	//修改文件
    public function editAction()
    {
        $this->view->title = "修改通知单";
		$this->view->headTitle($this->view->title,'PREPEND');
	
		$form = new Form_EditJswjggtzd();
		$form->submit->setLabel('修改');
		$this->view->form = $form;
		
		if($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
			if($form->isValid($formData)){
				$id           = $form->getValue('id');
				$old_id       = $form->getValue('old_id');
				$drawing_num  = $form->getValue('drawing_num');
				$file_name    = $form->getValue('file_name');
				$file_content = $form->getValue('file_content');
				$use_for      = $form->getValue('use_for');
				$add_date     = $form->getValue('add_date');
				$old_scan_img = $form->getValue('old_scan_img');
				$old_pdf_file = $form->getValue('old_pdf_file');
				
				
				$new_name = date('YmdHis');
				$scan_img = '';
				$pdf = '';
				$target_dir = '';
				$target_file = '';
				
				//更新扫描件
				if(substr_count($old_scan_img,'jpg')>0){
					$temp_arr = split('[/]',$old_scan_img);
					$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$temp_arr[2];
					$target_file = $temp_arr[3];
					$scan_img = 'doc/jswjggtzd/'.$temp_arr[2].'/'.$target_file;
				}else{
					if($old_pdf_file!=''){
						$temp_arr = split('[/]',$old_pdf_file);
						$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$temp_arr[2];
						$target_file = $new_name.'.jpg';
						$scan_img = 'doc/jswjggtzd/'.$temp_arr[2].'/'.$target_file;
					}else{
						$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$old_id.'-'.$new_name;
						$target_file = $new_name.'.jpg';
						mkdir($target_dir,0777);
						$scan_img = 'doc/jswjggtzd/'.$old_id.'-'.$new_name.'/'.$target_file;
					}
				}
				$form->new_scan_img->addFilter('Rename',
										   array('target'    => $target_dir.'/'.$target_file,
										         'overwrite' => true));
				//更新PDF
				if(substr_count($old_pdf_file,'pdf')>0){
					$temp_arr = split('[/]',$old_pdf_file);
					$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$temp_arr[2];
					$target_file = $temp_arr[3];
					$pdf = $old_pdf_file;
				}else{
					if(substr_count($old_scan_img,'jpg')>0){
						$temp_arr = split('[/]',$old_scan_img);
						$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$temp_arr[2];
						$target_file = $new_name.'.pdf';
						$pdf = 'doc/jswjggtzd/'.$temp_arr[2].'/'.$target_file;
					}else{
						$target_dir  = 'd:/dms/public/doc/jswjggtzd/'.$old_id.'-'.$new_name;
						mkdir($target_dir,0777);
						$target_file = $new_name.'.pdf';
						$pdf = 'doc/jswjggtzd/'.$old_id.'-'.$new_name.'/'.$target_file;
					}	
				}
				$form->pdf_file->addFilter('Rename',
										   array('target'    => $target_dir.'/'.$target_file,
										         'overwrite' => true));
				
				//接收文件
				$form->getValue('new_scan_img');
				$form->getValue('pdf_file');

				$this->jswjggtzd->updateFile($id,
											 $drawing_num,
											 $file_name,
											 $file_content,
											 $use_for,
											 $add_date,
											 $scan_img,
											 $pdf
											 );
				$this->_redirect('/jswjggtzd/index');
			}else{
				$form->populate($formData);
			}
		}else{
			$id = (int)$this->_getParam('id',0);
			$the_file = $this->jswjggtzd->getFile($id);
			//填充原值
			$form->id->setValue($id);
			$form->old_id->setValue($the_file['old_id']);
			$form->drawing_num->setValue($the_file['drawing_num']);
			$form->file_name->setValue($the_file['file_name']);
			$form->file_content->setValue($the_file['file_content']);
			$form->use_for->setValue($the_file['use_for']);
			$form->add_date->setValue($the_file['add_date']);
			$form->old_scan_img->setValue($the_file['scan_img']);
			$form->old_pdf_file->setValue($the_file['pdf_file']);

			//Zend_Debug::dump($temp_arr,'array');
		}
    }
	//删除文件
    public function deleteAction()
    {
        // action body
		$ids = $this->_request->getParam('file_checks');
		$del_id = array();
		foreach($ids as $id){
			$tmp_file = $this->jswjggtzd->getFile($id);
			$temp_arr = split('[/]',$tmp_file['scan_img']);
			if($temp_arr == ''){
				$temp_arr = split('[/]',$tmp_file['pdf_file']);
			}
			if($temp_arr != ""){
				array_push($del_id,$id);
				//删除记录
				$this->jswjggtzd->deleteFile($id);
				//删除目录
				if($temp_arr[2]!=''){
					$this->tools->delDir('d:/dms/public/doc/jswjggtzd/'.$temp_arr[2]);
				}
			}
		}
		$json_string = json_encode($del_id);
		echo $json_string;

		
    }
}









